package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"purchase-mall/services"
	"purchase-mall/services/service"
)

// TokenListByLokiHandler 获取token请求列表
func TokenListByLokiHandler(c *gin.Context) {
	var tokenListSearch service.TokenListSearch
	err := c.ShouldBindJSON(&tokenListSearch)
	if err != nil {
		zap.L().Error("get tokenList params failed", zap.Error(err))
	}

	if tokenListSearch.PageNum == 0 {
		tokenListSearch.PageNum = 1
	}

	if tokenListSearch.PageSize == 0 {
		tokenListSearch.PageSize = 10
	}

	data, err := services.GetTokenListService(&tokenListSearch)
	if err != nil {
		zap.L().Error("services.GetTokenListService() failed", zap.Error(err))
		ResponseError(c, CodeServerBusy)
		return
	}
	ResponseSuccess(c, data)
}

// RequestTop20Handler 获取token请求数top20
func RequestTop20Handler(c *gin.Context) {
	var top20P service.Top20Search
	err := c.ShouldBindJSON(&top20P)
	if err != nil {
		zap.L().Error("get top20 params failed", zap.Error(err))
	}

	if top20P.PageNum == 0 {
		top20P.PageNum = 1
	}

	if top20P.PageSize == 0 {
		top20P.PageSize = 10
	}

	data, err := services.GetRequestTop20Service(&top20P)
	if err != nil {
		zap.L().Error("services.GetRequestTop20Service() failed", zap.Error(err))
		ResponseError(c, CodeServerBusy)
		return
	}
	ResponseSuccess(c, data)
	// 返回响应
}

// TokenListHandler 获取token列表的处理函数
func TokenListHandler(c *gin.Context) {
	// 获取分页参数
	page, size, filters := getPramsInfo(c)
	println(page)
	println(size)
	fmt.Println(filters)
	//fmt.Println(filters["aa"])
	// 获取数据
	data, err := services.GetTokenList(page, size, filters)
	if err != nil {
		zap.L().Error("services.GetTokenList() failed", zap.Error(err))
		ResponseError(c, CodeServerBusy)
		return
	}
	ResponseSuccess(c, data)
	// 返回响应
}

// TableListHandler 获取token列表的处理函数
func TableListHandler(c *gin.Context) {
	// 获取分页参数
	page, size, _ := getPramsInfo(c)
	//println(filters)
	// 获取数据
	data, err := services.GetTableList(page, size)
	if err != nil {
		zap.L().Error("services.GetTableList() failed", zap.Error(err))
		ResponseError(c, CodeServerBusy)
		return
	}
	ResponseSuccess(c, data)
	// 返回响应
}
